home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.04 Apr 94 / Accurate Timing / TStats / PlotLongArray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-10  |  2.6 KB  |  159 lines  |  [TEXT/KAHL]

  1. /* PlotLongArray -------------------------------------------------
  2.  *
  3.  * Plot array of longs.
  4.  *
  5.  * Copyright (c) 1993 Bill Karsh.
  6.  * All rights reserved.
  7.  *
  8.  */
  9.  
  10.  
  11. #pragma options( honor_register, !assign_registers )
  12.  
  13.  
  14. #include    "PlotLongArray.h"
  15.  
  16. #define        TextMargin    1
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. /* PlotLongArray -------------------------------------------------
  24.  *
  25.  * Plot array of N longs.
  26.  *
  27.  * min and max values set the scales.
  28.  * r bounds the entire plot including labels.
  29.  *
  30.  * Copyright (c) 1993 Bill Karsh.
  31.  * All rights reserved.
  32.  *
  33.  */
  34.  
  35.  
  36. #pragma options( honor_register, !assign_registers )
  37.  
  38.  
  39. #include    "PlotLongArray.h"
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. void PlotLongArray(
  47.     register long    *data,
  48.     long            N,
  49.     long            hMin,
  50.     long            hMax,
  51.     long            vMin,
  52.     long            vMax,
  53.     Rect            *r,
  54.     StringPtr        title )
  55. {
  56.     register short    v0, wHi, hScale, vScale;
  57.     register long    i, sum;
  58.     short            h, wWid, sMinWid, sMaxWid;
  59.     Rect            R;
  60.     Point            p;
  61.     Byte            sMin[16], sMax[16];
  62.     FontInfo        fi;
  63.         
  64. // adjust vMin and vMax
  65.  
  66.     if( vMin > 0 && vMax > 0 )
  67.         vMin = 0;
  68.     else if( vMin < 0 && vMax < 0 )
  69.         vMax = 0;
  70.         
  71.     R = *r;
  72.     EraseRect( &R );
  73.     
  74. // make room for labels on left and bottom
  75.     
  76.     NumToString( vMin, sMin );
  77.     NumToString( vMax, sMax );
  78.     GetFontInfo( &fi );
  79.     sMinWid = StringWidth( sMin );
  80.     sMaxWid = StringWidth( sMax );
  81.     h = sMaxWid;
  82.     if( sMinWid > h ) h = sMinWid;
  83.     
  84.     R.left        += h + TextMargin*2;
  85.     R.bottom    -= fi.ascent + fi.descent + TextMargin;
  86.     
  87.     ForeColor( greenColor );
  88.     FrameRect( &R );
  89.     InsetRect( &R, 1, 1 );
  90.  
  91. // vert labels
  92.     
  93.     ForeColor( blackColor );
  94.     MoveTo( R.left - sMaxWid - TextMargin,
  95.             R.top + fi.ascent );
  96.     DrawString( sMax );
  97.     
  98.     MoveTo( R.left - sMinWid - TextMargin,
  99.             R.bottom - fi.descent );
  100.     DrawString( sMin );
  101.     
  102. // horiz labels
  103.     
  104.     v0 = R.bottom + fi.ascent + TextMargin;
  105.     NumToString( hMin, sMin );
  106.     MoveTo( R.left + TextMargin, v0 );
  107.     DrawString( sMin );
  108.     
  109.     NumToString( hMax, sMax );
  110.     MoveTo( R.right - StringWidth( sMax ) - TextMargin, v0 );
  111.     DrawString( sMax );
  112.     
  113.     if( title ) {
  114.         MoveTo((R.right + R.left - StringWidth(title))/2, v0);
  115.         DrawString( title );
  116.     }
  117.     
  118. // get ready to plot
  119.  
  120.     hScale = hMax - hMin;
  121.     vScale = vMax - vMin;
  122.  
  123.     if( !hScale || !vScale ) return;
  124.     
  125.     wWid    = R.right - R.left;
  126.     wHi        = R.bottom - R.top;
  127.  
  128. // draw absissa at v = 0
  129.     
  130.     v0 = R.top + (vMax * wHi) / vScale;
  131.     
  132.     MoveTo( R.left, v0 );
  133.     LineTo( R.right - 1, v0 );
  134.     
  135. // draw data
  136.  
  137.     sum = 0;
  138.     
  139.     for( i = 0; i < N; i++ ) {
  140.     
  141.         h = R.left + (i * wWid) / hScale;
  142.         
  143.         MoveTo( h, v0 );
  144.         LineTo( h, v0 - (*data * wHi) / vScale );
  145.         
  146.         sum += *data;
  147.         data++;
  148.     }
  149.  
  150. // draw average v line
  151.  
  152.     v0 -= (sum/N * wHi) / vScale;
  153.     
  154.     ForeColor( redColor );
  155.     MoveTo( R.left, v0 );
  156.     LineTo( R.right - 1, v0 );
  157.     
  158.     ForeColor( blackColor );
  159. }